package org.yt.dao.base;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.yt.util.ConnFactory;

public abstract class DefaultDao implements BaseDao {

	public void add(String sql) {
		Statement stmt = null;
		try {
			stmt = ConnFactory.getConn().createStatement();
			stmt.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void delete(String sql) {
		Statement stmt = null;
		try {
			stmt = ConnFactory.getConn().createStatement();
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public ResultSet findAll(String sql) {
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = ConnFactory.getConn().createStatement();
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return rs;
	}

	public ResultSet findById(String sql) {
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = ConnFactory.getConn().createStatement();
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return rs;
	}

	public ResultSet find(String sql) {
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = ConnFactory.getConn().createStatement();
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
	
	public void update(String sql) {
		Statement stmt = null;
		try {
			stmt = ConnFactory.getConn().createStatement();
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
